home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / PRTPerVertex / skybox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  7.0 KB  |  201 lines

  1. //-----------------------------------------------------------------------------
  2. // File: skybox.cpp
  3. //
  4. // Desc: Encapsulation of skybox geometry and textures
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #include "dxstdafx.h"
  9. #include "skybox.h"
  10.  
  11. //#define DEBUG_VS   // Uncomment this line to debug vertex shaders 
  12. //#define DEBUG_PS   // Uncomment this line to debug pixel shaders 
  13.  
  14. struct SKYBOX_VERTEX
  15. {
  16.     D3DXVECTOR4 pos;
  17.     D3DXVECTOR3 tex;
  18. };
  19.  
  20.  
  21. D3DVERTEXELEMENT9 g_aSkyboxDecl[] =
  22. {
  23.     { 0, 0,  D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  24.     D3DDECL_END()
  25. };
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Name: CSkybox
  30. // Desc: Constructor
  31. //-----------------------------------------------------------------------------
  32. CSkybox::CSkybox()
  33. {
  34.     m_pVB               = NULL;
  35.     m_pd3dDevice        = NULL;
  36.     m_pEffect           = NULL;
  37.     m_pVertexDecl       = NULL;
  38.     m_pEnvironmentMap   = NULL;
  39.     m_pEnvironmentMapSH = NULL;
  40.     m_fSize             = 1.0f;
  41.     m_bDrawSH           = false;
  42. }
  43.  
  44.  
  45. //-----------------------------------------------------------------------------
  46. HRESULT CSkybox::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice, float fSize, LPDIRECT3DCUBETEXTURE9 pEnvMap, WCHAR* strEffectFileName )
  47. {
  48.     HRESULT hr;
  49.  
  50.     m_pd3dDevice = pd3dDevice;
  51.     m_fSize = fSize;
  52.     m_pEnvironmentMap = pEnvMap;
  53.  
  54.     // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the shader debugger.  
  55.     // Debugging vertex shaders requires either REF or software vertex processing, and debugging 
  56.     // pixel shaders requires REF.  The D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug 
  57.     // experience in the shader debugger.  It enables source level debugging, prevents instruction 
  58.     // reordering, prevents dead code elimination, and forces the compiler to compile against the next 
  59.     // higher available software target, which ensures that the unoptimized shaders do not exceed 
  60.     // the shader model limitations.  Setting these flags will cause slower rendering since the shaders 
  61.     // will be unoptimized and forced into software.  See the DirectX documentation for more information 
  62.     // about using the shader debugger.
  63.     DWORD dwShaderFlags = 0;
  64.     #ifdef DEBUG_VS
  65.         dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
  66.     #endif
  67.     #ifdef DEBUG_PS
  68.         dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
  69.     #endif
  70.         
  71.     // Read the D3DX effect file
  72.     WCHAR str[MAX_PATH];
  73.     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strEffectFileName ) );
  74.  
  75.     // If this fails, there should be debug output as to 
  76.     // they the .fx file failed to compile
  77.     V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, 
  78.                                         dwShaderFlags, NULL, &m_pEffect, NULL ) );
  79.  
  80.     // Create vertex declaration
  81.     V_RETURN( pd3dDevice->CreateVertexDeclaration( g_aSkyboxDecl, &m_pVertexDecl ) );
  82.  
  83.     return S_OK;
  84. }
  85.  
  86. void CSkybox::InitSH(LPDIRECT3DCUBETEXTURE9 pSHTex)
  87. {
  88.     m_pEnvironmentMapSH = pSHTex;
  89. }
  90.  
  91.  
  92. //-----------------------------------------------------------------------------
  93. HRESULT CSkybox::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice, float fSize, WCHAR* strCubeMapFile, WCHAR* strEffectFileName )
  94. {
  95.     HRESULT hr;
  96.  
  97.     WCHAR strPath[MAX_PATH];
  98.     LPDIRECT3DCUBETEXTURE9 pEnvironmentMap;
  99.     V_RETURN( DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, strCubeMapFile ) );
  100.     V_RETURN( D3DXCreateCubeTextureFromFileEx( pd3dDevice, strPath, D3DX_DEFAULT, 1, 0, D3DFMT_A16B16G16R16F, 
  101.                                                D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pEnvironmentMap ) );
  102.  
  103.     V_RETURN( OnCreateDevice( pd3dDevice, fSize, pEnvironmentMap, strEffectFileName ) );
  104.  
  105.     return S_OK;
  106. }
  107.  
  108. //-----------------------------------------------------------------------------
  109. void CSkybox::OnResetDevice( const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
  110. {
  111.     HRESULT hr;
  112.  
  113.     if( m_pEffect )
  114.         V( m_pEffect->OnResetDevice() );
  115.  
  116.     V( m_pd3dDevice->CreateVertexBuffer( 4 * sizeof(SKYBOX_VERTEX), 
  117.                                          D3DUSAGE_WRITEONLY, 0, 
  118.                                          D3DPOOL_DEFAULT, &m_pVB, NULL ) );
  119.  
  120.     // Fill the vertex buffer
  121.     SKYBOX_VERTEX* pVertex = NULL;
  122.     V( m_pVB->Lock( 0, 0, (void**)&pVertex, 0 ) );
  123.  
  124.     // Map texels to pixels 
  125.     float fHighW = -1.0f - (1.0f/(float)pBackBufferSurfaceDesc->Width);
  126.     float fHighH = -1.0f - (1.0f/(float)pBackBufferSurfaceDesc->Height);
  127.     float fLowW  = 1.0f + (1.0f/(float)pBackBufferSurfaceDesc->Width);
  128.     float fLowH  = 1.0f + (1.0f/(float)pBackBufferSurfaceDesc->Height);
  129.  
  130.     pVertex[0].pos = D3DXVECTOR4(fLowW, fLowH, 1.0f, 1.0f);
  131.     pVertex[1].pos = D3DXVECTOR4(fLowW, fHighH, 1.0f, 1.0f);
  132.     pVertex[2].pos = D3DXVECTOR4(fHighW, fLowH, 1.0f, 1.0f);
  133.     pVertex[3].pos = D3DXVECTOR4(fHighW, fHighH, 1.0f, 1.0f);
  134.  
  135.     m_pVB->Unlock();
  136. }
  137.  
  138.  
  139. //-----------------------------------------------------------------------------
  140. // Name: Render
  141. // Desc: 
  142. //-----------------------------------------------------------------------------
  143. void CSkybox::Render( D3DXMATRIX* pmWorldViewProj, float fAlpha, float fScale )
  144. {
  145.     HRESULT hr;
  146.  
  147.     D3DXMATRIX mInvWorldViewProj;
  148.     D3DXMatrixInverse( &mInvWorldViewProj, NULL, pmWorldViewProj );
  149.     V( m_pEffect->SetMatrix( "g_mInvWorldViewProjection", &mInvWorldViewProj ) );
  150.         
  151.     if ((fScale == 0.0f) || (fAlpha == 0.0f)) return; // do nothing if no intensity...
  152.  
  153.     // Draw the skybox
  154.     UINT uiPass, uiNumPasses;
  155.     V( m_pEffect->SetTechnique( "Skybox" ) );
  156.     V( m_pEffect->SetFloat( "g_fAlpha", fAlpha ) );
  157.     V( m_pEffect->SetFloat( "g_fScale", fAlpha*fScale ) );
  158.  
  159.     if (this->m_bDrawSH) {
  160.         V( m_pEffect->SetTexture( "g_EnvironmentTexture", m_pEnvironmentMapSH ) );
  161.     } else {
  162.         V( m_pEffect->SetTexture( "g_EnvironmentTexture", m_pEnvironmentMap ) );
  163.     }
  164.  
  165.     m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(SKYBOX_VERTEX) );
  166.     m_pd3dDevice->SetVertexDeclaration( m_pVertexDecl );
  167.  
  168.     V( m_pEffect->Begin( &uiNumPasses, 0 ) );
  169.     for( uiPass=0; uiPass < uiNumPasses; uiPass++ )
  170.     {
  171.         V( m_pEffect->BeginPass( uiPass ) );      
  172.         m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  173.         V( m_pEffect->EndPass() );
  174.     }
  175.     V( m_pEffect->End() );
  176. }
  177.  
  178.  
  179. //-----------------------------------------------------------------------------
  180. void CSkybox::OnLostDevice()
  181. {
  182.     HRESULT hr;
  183.     if( m_pEffect )
  184.         V( m_pEffect->OnLostDevice() );
  185.     SAFE_RELEASE( m_pVB );
  186. }
  187.  
  188.  
  189. //-----------------------------------------------------------------------------
  190. void CSkybox::OnDestroyDevice()
  191. {
  192.     SAFE_RELEASE( m_pEnvironmentMap );
  193.     SAFE_RELEASE( m_pEnvironmentMapSH );
  194.     SAFE_RELEASE( m_pEffect );
  195.     SAFE_RELEASE( m_pVertexDecl );
  196. }
  197.  
  198.  
  199.  
  200.  
  201.